package com.tongshu.multiplefiles.controller;

import com.tongshu.multiplefiles.config.Constant;
import com.tongshu.multiplefiles.config.UploadConfig;
import com.tongshu.multiplefiles.model.MyFile;
import com.tongshu.multiplefiles.service.FileService;
import com.tongshu.multiplefiles.utils.FileMd5Util;
import com.tongshu.multiplefiles.utils.FileUtils;
import com.tongshu.multiplefiles.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.tongshu.multiplefiles.utils.UploadUtils.*;

@Controller
@CrossOrigin
@RequestMapping("/File")
public class UploadController {
    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public void upload(MultipartFile file) throws IOException {
        String md5 = "0";
        fileService.uploadFile(file.getOriginalFilename(), md5, file);
    }

    @ResponseBody
    @PostMapping("/isUpload")
    public Map<String, Object> isUpload(String filemd5, Integer chunkNum) {
        /**
         * 逻辑：
         * 1. 首先判断当前文件的md5是否已存在，即当前文件是否已上传
         * 2. 已上传则返回状态为已上传
         * 3. 未上传则返回状态为未上传，并于数据库记录
         */
        return fileService.isUpload1(filemd5, chunkNum);
    }

    @ResponseBody
    @PostMapping("/bigUpload")
    public Map<String, Object> bigUpload(String name,
                                         String filemd5,
                                         String chunkMd5,
                                         String filename,
                                         Long fileSize,
                                         Integer chunkNum,
                                         Integer index,
                                         MultipartFile fileData) throws IOException {
        Map<String, Object> map = new HashMap<>();
        // 检查是否所有分块已上传，并合并
        if (checkAllUploaded(filemd5, filename, fileSize, chunkNum)) {
            map.put("flag", 0); // 所有分块已上传并合并
            return map;
        }

        // 检查当前分块是否已上传，未上传
        System.out.println(filemd5 + "===" + chunkMd5 + "====" + chunkNum + "===" + index);
        String chunkName = getFileName(filemd5, chunkMd5, chunkNum, index);
        //FileUtils.writeWithBlock(UploadConfig.tempPath + fileName, fileSize, file.getInputStream(), file.getSize(), chunkNum, index);
        FileUtils.writeFile(UploadConfig.tempPath + chunkName, fileData.getInputStream());
        chunkSuccess(filemd5, index);
        fileService.saveTempChunk(filemd5, chunkMd5, new Date(), Constant.TempFile.STATUS_SUCCESS, index);
        // 检查是否所有分块已上传并合并，是则返回0，否则返回2
        map.put("flag", checkAllUploaded(filemd5, filename, fileSize, chunkNum) ? 0 : 2); // 此前分块未上传，现已上传
        return map;
    }

    // 检查是否所有分块已上传完毕
    private boolean checkAllUploaded(String filemd5,
                                     String filename,
                                     Long fileSize,
                                     Integer chunkNum) throws IOException {
        // 检查是否所有分块已上传
        if (isUploaded(filemd5)) {
            boolean mergeStatus = mergeChunks(UploadConfig.filePath + filename, fileSize, chunkNum, filemd5);
            if (mergeStatus) {
                fileService.saveMyFile(filename, UploadConfig.filePath + filename, filemd5, new Date(), Constant.MyFile.STATUS_SUCCESS);
                return true;
            }
        }
        return false;
    }


}
